<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>迭代器模式</title>
</head>
<body>
    <script>
        // 外部迭代器
        var Iterator = function(obj) {
            var current = 0;
            var next = function() {
                current++;
            }
            var isDone = function() {
                return current >= obj.length
            }
            var getCurrentItem = function() {
                return obj[current]
            }
            return {
                next,
                isDone,
                getCurrentItem
            }
        }

        var iterator = new Iterator([1,2,3])
        // console.log(iterator.getCurrentItem())
        // iterator.next()
        // console.log(iterator.getCurrentItem())
        // console.log(iterator.getCurrentItem())
        // iterator.next()
        // console.log(iterator.getCurrentItem())

        while(!iterator.isDone()) {
            console.log(iterator.getCurrentItem())
            iterator.next();
        }


    </script>
</body>
</html>